* s~\t+$~~
[lhc/web/wiklou.git] / includes / Title.php
index 0d5d1c7..5951552 100644 (file)
@@ -161,14 +161,17 @@ class Title {
         * @access public
         */
        function newFromURL( $url ) {
+               global $wgLegalTitleChars;
                $t = new Title();
 
-               # For compatibility with old buggy URLs. "+" is not valid in titles,
+               # For compatibility with old buggy URLs. "+" is usually not valid in titles,
                # but some URLs used it as a space replacement and they still come
                # from some external search tools.
-               $s = str_replace( '+', ' ', $url );
+               if ( strpos( $wgLegalTitleChars, '+' ) === false ) {
+                       $url = str_replace( '+', ' ', $url );
+               }
 
-               $t->mDbkeyform = str_replace( ' ', '_', $s );
+               $t->mDbkeyform = str_replace( ' ', '_', $url );
                if( $t->secureAndSplit() ) {
                        return $t;
                } else {
@@ -545,7 +548,7 @@ class Title {
         * @return string
         * @access public
         */
-       function getNsText() { 
+       function getNsText() {
                global $wgContLang;
                return $wgContLang->getNsText( $this->mNamespace );
        }
@@ -709,7 +712,7 @@ class Title {
                                global $wgActionPaths;
                                $url = false;
                                if( !empty( $wgActionPaths ) &&
-                                       preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) 
+                                       preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) )
                                {
                                        $action = urldecode( $matches[2] );
                                        if( isset( $wgActionPaths[$action] ) ) {
@@ -769,7 +772,9 @@ class Title {
         */
        function getInternalURL( $query = '' ) {
                global $wgInternalServer;
-               return $wgInternalServer . $this->getLocalURL( $query );
+               $url = $wgInternalServer . $this->getLocalURL( $query );
+               wfRunHooks( 'GetInternalURL', array( &$this, &$url, $query ) );
+               return $url;
        }
 
        /**
@@ -864,7 +869,7 @@ class Title {
                        wfProfileOut( $fname );
                        return false;
                }
-               
+
                if( $this->mDbkeyform == '_' ) {
                        # FIXME: Is this necessary? Shouldn't be allowed anyway...
                        wfProfileOut( $fname );
@@ -906,7 +911,7 @@ class Title {
                        wfProfileOut( $fname );
                        return false;
                }
-               
+
                if( $action == 'create' ) {
                        if( (  $this->isTalkPage() && !$wgUser->isAllowed( 'createtalk' ) ) ||
                                ( !$this->isTalkPage() && !$wgUser->isAllowed( 'createpage' ) ) ) {
@@ -1097,14 +1102,14 @@ class Title {
         * @access public
         */
        function getArticleID( $flags = 0 ) {
-               global $wgLinkCache;
+               $linkCache =& LinkCache::singleton();
                if ( $flags & GAID_FOR_UPDATE ) {
-                       $oldUpdate = $wgLinkCache->forUpdate( true );
-                       $this->mArticleID = $wgLinkCache->addLinkObj( $this );
-                       $wgLinkCache->forUpdate( $oldUpdate );
+                       $oldUpdate = $linkCache->forUpdate( true );
+                       $this->mArticleID = $linkCache->addLinkObj( $this );
+                       $linkCache->forUpdate( $oldUpdate );
                } else {
                        if ( -1 == $this->mArticleID ) {
-                               $this->mArticleID = $wgLinkCache->addLinkObj( $this );
+                               $this->mArticleID = $linkCache->addLinkObj( $this );
                        }
                }
                return $this->mArticleID;
@@ -1123,7 +1128,7 @@ class Title {
 
        /**
         * This clears some fields in this object, and clears any associated
-        * keys in the "bad links" section of $wgLinkCache.
+        * keys in the "bad links" section of the link cache.
         *
         * - This is called from Article::insertNewArticle() to allow
         * loading of the new page_id. It's also called from
@@ -1133,8 +1138,8 @@ class Title {
         * @access public
         */
        function resetArticleID( $newid ) {
-               global $wgLinkCache;
-               $wgLinkCache->clearBadLink( $this->getPrefixedDBkey() );
+               $linkCache =& LinkCache::singleton();
+               $linkCache->clearBadLink( $this->getPrefixedDBkey() );
 
                if ( 0 == $newid ) { $this->mArticleID = -1; }
                else { $this->mArticleID = $newid; }
@@ -1398,8 +1403,8 @@ class Title {
         * @return array the Title objects linking here
         * @access public
         */
-       function getLinksTo( $options = '' ) {
-               global $wgLinkCache;
+       function getLinksTo( $options = '', $table = 'pagelinks', $prefix = 'pl' ) {
+               $linkCache =& LinkCache::singleton();
                $id = $this->getArticleID();
 
                if ( $options ) {
@@ -1408,12 +1413,12 @@ class Title {
                        $db =& wfGetDB( DB_SLAVE );
                }
 
-               $res = $db->select( array( 'page', 'pagelinks' ),
+               $res = $db->select( array( 'page', $table ),
                        array( 'page_namespace', 'page_title', 'page_id' ),
                        array(
-                               'pl_from=page_id',
-                               'pl_namespace' => $this->getNamespace(),
-                               'pl_title'     => $this->getDbKey() ),
+                               "{$prefix}_from=page_id",
+                               "{$prefix}_namespace" => $this->getNamespace(),
+                               "{$prefix}_title"     => $this->getDbKey() ),
                        'Title::getLinksTo',
                        $options );
 
@@ -1421,7 +1426,7 @@ class Title {
                if ( $db->numRows( $res ) ) {
                        while ( $row = $db->fetchObject( $res ) ) {
                                if ( $titleObj = Title::makeTitle( $row->page_namespace, $row->page_title ) ) {
-                                       $wgLinkCache->addGoodLinkObj( $row->page_id, $titleObj );
+                                       $linkCache->addGoodLinkObj( $row->page_id, $titleObj );
                                        $retVal[] = $titleObj;
                                }
                        }
@@ -1430,6 +1435,18 @@ class Title {
                return $retVal;
        }
 
+       /**
+        * Get an array of Title objects using this Title as a template
+        * Also stores the IDs in the link cache.
+        *
+        * @param string $options may be FOR UPDATE
+        * @return array the Title objects linking here
+        * @access public
+        */
+       function getTemplateLinksTo( $options = '' ) {
+               return $this->getLinksTo( $options, 'templatelinks', 'tl' );
+       }
+
        /**
         * Get an array of Title objects referring to non-existent articles linked from this page
         *
@@ -1438,8 +1455,6 @@ class Title {
         * @access public
         */
        function getBrokenLinksFrom( $options = '' ) {
-               global $wgLinkCache;
-
                if ( $options ) {
                        $db =& wfGetDB( DB_MASTER );
                } else {
@@ -1628,7 +1643,7 @@ class Title {
         * @access private
         */
        function moveOverExistingRedirect( &$nt, $reason = '' ) {
-               global $wgUser, $wgLinkCache, $wgUseSquid, $wgMwRedir;
+               global $wgUser, $wgUseSquid, $wgMwRedir;
                $fname = 'Title::moveOverExistingRedirect';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
 
@@ -1641,6 +1656,7 @@ class Title {
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
+               $linkCache =& LinkCache::singleton();
 
                # Delete the old redirect. We don't save it to history since
                # by definition if we've got here it's rather uninteresting.
@@ -1665,7 +1681,7 @@ class Title {
                        /* WHERE */ array( 'page_id' => $oldid ),
                        $fname
                );
-               $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
+               $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
                # Recreate the redirect, this time in the other direction.
                $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
@@ -1677,7 +1693,7 @@ class Title {
                        'text'    => $redirectText ) );
                $revid = $redirectRevision->insertOn( $dbw );
                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-               $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
+               $linkCache->clearLink( $this->getPrefixedDBkey() );
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -1708,7 +1724,7 @@ class Title {
         * @access private
         */
        function moveToNewTitle( &$nt, &$newid, $reason = '' ) {
-               global $wgUser, $wgLinkCache, $wgUseSquid;
+               global $wgUser, $wgUseSquid;
                global $wgMwRedir;
                $fname = 'MovePageForm::moveToNewTitle';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
@@ -1720,8 +1736,8 @@ class Title {
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
                $now = $dbw->timestamp();
-               wfSeedRandom();
                $rand = wfRandom();
+               $linkCache =& LinkCache::singleton();
 
                # Save a null revision in the page's history notifying of the move
                $nullRevision = Revision::newNullRevision( $dbw, $oldid,
@@ -1741,7 +1757,7 @@ class Title {
                        $fname
                );
 
-               $wgLinkCache->clearLink( $nt->getPrefixedDBkey() );
+               $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
                # Insert redirect
                $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
@@ -1753,7 +1769,7 @@ class Title {
                        'text'    => $redirectText ) );
                $revid = $redirectRevision->insertOn( $dbw );
                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-               $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
+               $linkCache->clearLink( $this->getPrefixedDBkey() );
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -2035,14 +2051,25 @@ class Title {
                                'pl_namespace' => $this->getNamespace(),
                                'pl_title'     => $this->getDbKey() ),
                        $fname );
-               if ( 0 == $dbw->numRows( $res ) ) {
-                       return;
-               }
 
                $toucharr = array();
                while( $row = $dbw->fetchObject( $res ) ) {
                        $toucharr[] = $row->pl_from;
                }
+               $dbw->freeResult( $res );
+
+               if( $this->getNamespace() == NS_CATEGORY ) {
+                       // Categories show up in a separate set of links as well
+                       $res = $dbw->select( 'categorylinks',
+                               array( 'cl_from' ),
+                               array( 'cl_to' => $this->getDbKey() ),
+                               $fname );
+                       while( $row = $dbw->fetchObject( $res ) ) {
+                               $toucharr[] = $row->cl_from;
+                       }
+                       $dbw->freeResult( $res );
+               }
+
                if (!count($toucharr))
                        return;
                $dbw->update( 'page', /* SET */ array( 'page_touched' => $dbw->timestamp() ),